<?php
/**
 * 时间段模型
 * @author jianhui
 *
 */
class timeUnitModel extends MY_Model{
    function timeUnitModel(){
        parent::MY_Model();
        $this->table = 'TimeUnit';
        $this->load->library('Cache');
    }
    /**
     * 包装数据(non-PHPdoc)
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        foreach ($list as $k => $v){
            $v['weekList'] = $v['weekList'] ? unserialize($v['weekList']) : array();
            $v['timeSetting'] = $v['timeSetting'] ? unserialize($v['timeSetting']) : array();
            $list[$k] = $v;
        }
        return $list;
    }
    /**
     * 缓存时间配置
     */
    function createTimeUnitCache(){
        $yearList = array();//年份列表
        $timeUnitList = array();//时段列表
        $timeUnitDataList = array();//按年排列时段列表
        $findList = $this->getNewList(array('id > '=>0), null, null, '', 'year asc,orderNum asc');
        foreach ($findList as $v){
            //年份列表
            isset($yearList[(int)$v['year']]) || $yearList[(int)$v['year']] = (int)$v['year'].'年';
            //时段列表
            isset($timeUnitList[(int)$v['year']]) || $timeUnitList[(int)$v['year']] = array();
            $timeUnitList[(int)$v['year']][(int)$v['id']] = $v['name'];
            //时段数据列表
            isset($timeUnitDataList[(int)$v['year']]) || $timeUnitDataList[(int)$v['year']] = array();
            $v['startTime'] = strtotime($v['startTime']);
            $v['endTime'] = strtotime($v['endTime']);
            
            $timeUnitDataList[(int)$v['year']][(int)$v['id']] = $v;
        }
        $this->cache->set('yearList', $yearList);//缓存年份列表
        $this->cache->set('timeUnitList', $timeUnitList);//缓存时段列表
        $this->cache->set('timeUnitDataList', $timeUnitDataList);//缓存时段数据
    }
    /**
     * 获取时段相关数据
     * @param string $type
     * yearList：年份列表
     * timeUnitList：时段列表
     * timeUnitDataList：时段数据
     * @return array
     */
    function getCacheData($type){
        $this->cache->expire = 86400;//设置过期时间为一天
        $data = $this->cache->get($type);
        if (!$data){
            //创建缓存
            $this->createTimeUnitCache();
            $data = $this->cache->get($type);
        }
        return $data;
    }
    /**
     * 获取当前时段ID
     * @return Ambigous <>
     */
    function getNowTimeUnit(){
        return $this->getTimeUnit(time());
    }
    /**
     * 获取指定时间戳的时段ID
     * @param number $timeStamp
     * @return Ambigous <>
     */
    function getTimeUnit($timeStamp = 0){
        $year = date('Y', $timeStamp);
        if ($year != $this->config->item('nowYear')) {
            $year = $this->config->item('nowYear');
            $timeStamp = mktime(0, 0, 0, 12, 31, $year);
        }
        $timeUnitDataList = $this->getCacheData('timeUnitDataList');
        $list = isset($timeUnitDataList[$year]) ? $timeUnitDataList[$year] : array();
        if ($list){
            foreach($list as $k => $v){
                if ($timeStamp > $v['startTime'] && $timeStamp < $v['endTime']){
                    return $v['id'];
                }
            }
        }
        return 0;
    }
    /**
     * 获取时段列表
     * @param number $year
     * @return Ambigous <multitype:, array>
     */
    function getTimeUnitList($year = 2016){
        $timeUnitList = $this->getCacheData('timeUnitList');
        return isset($timeUnitList[$year]) ? $timeUnitList[$year] : array();
    }
    /**
     * 获取周列表
     * @param number $year
     * @param number $timeUnit
     * @return mixed
     */
    function getWeekList($year = 2016,$timeUnit=1){
        $timeUnitDataList = $this->getCacheData('timeUnitDataList');
        return isset($timeUnitDataList[$year][$timeUnit]) ? $timeUnitDataList[$year][$timeUnit]['weekList'] : array();
    }
    /**
     * 获取指定时段开始时间戳
     * @param int $timeUnit 时段ID
     * @param int $year 年
     */
    function getTimeUnitStartTime($timeUnit, $year){
        $timeUnitList = $this->getCacheData('timeUnitDataList');
        return isset($timeUnitList[$year][$timeUnit]) ? $timeUnitList[$year][$timeUnit]['startTime'] : time();
    }
    /**
     * 获取指定时段开始日期
     * @param int $timeUnit
     * @param int $year
     * @return string
     */
    function getTimeUnitStartDate($timeUnit, $year){
        $startTime = $this->getTimeUnitStartTime($timeUnit, $year);
        return date("Y-m-d", $startTime);
    }
    /**
     * 获取指定时段结束时间戳
     * @param int $timeUnit 时段ID
     * @param int $year 年
     * @return int
     */
    function getTimeUnitEndTime($timeUnit, $year){
        $timeUnitList = $this->getCacheData('timeUnitDataList');
        return isset($timeUnitList[$year][$timeUnit]) ? $timeUnitList[$year][$timeUnit]['endTime'] : time();
    }
    /**
     * 获取指定时段结束日期
     * @param int $timeUnit 时段ID
     * @param int $year 年
     * @return string
     */
    function getTimeUnitEndDate($timeUnit, $year){
        $endTime = $this->getTimeUnitEndTime($timeUnit, $year);
        return date("Y-m-d", $endTime);
    }
    /**
     * 获取指定时段的上个时段ID
     * @param int $year
     * @param int $timeUnit
     * @return number
     */
    function getPreTimeUnit($year, $timeUnit){
        $timeUnitList = $this->getCacheData('timeUnitList');
        if (isset($timeUnitList[$year])){
            $timeUnitIds = array_keys($timeUnitList[$year]);
            if (in_array($timeUnit, $timeUnitIds)){
                $key = array_search($timeUnit, $timeUnitIds);
                return isset($timeUnitIds[$key-1]) ? $timeUnitIds[$key-1] : 0;
            }
        }
        return 0;
    }
    /**
     * 获取指定时段的下个时段ID
     * @param int $year
     * @param int $timeUnit
     * @return number
     */
    function getNextTimeUnit($year, $timeUnit){
        $timeUnitList = $this->getCacheData('timeUnitList');
        if (isset($timeUnitList[$year])){
            $timeUnitIds = array_keys($timeUnitList[$year]);
            if (in_array($timeUnit, $timeUnitIds)){
                $key = array_search($timeUnit, $timeUnitIds);
                return isset($timeUnitIds[$key+1]) ? $timeUnitIds[$key+1] : 0;
            }
        }
        return 0;
    }
    /**
     * 通过周数获取周列表
     * @param number $weekNum
     * @return string[]
     */
    function getWeekNumList($weekNum = 0){
        $weekCon = array(
            1 => '第一周',
            2 => '第二周',
            3 => '第三周',
            4 => '第四周',
            5 => '第五周',
            6 => '第六周',
            7 => '第七周',
            8 => '第八周',
            9 => '第九周',
            10 => '第十周',
            11 => '第十一周',
            12 => '第十二周',
            13 => '第十三周',
            14 => '第十四周',
            15 => '第十五周',
            16 => '第十六周',
            17 => '第十七周',
            18 => '第十八周',
            19 => '第十九周',
            20 => '第二十周',
        );
        $data = array();
        for ($i = 1; $i <= $weekNum; $i++){
            $data[$i] = isset($weekCon[$i]) ? $weekCon[$i] : '';
        }
        return $data;
    }
}